package com.intellagric.service.agritech.impl;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.dao.agritech.AgtSpeciesDao;
import com.intellagric.mapper.AgtSpeciesMapper;
import com.intellagric.module.agritech.AgtSpeciesVo;
import com.intellagric.pojo.AgtSpecies;
import com.intellagric.pojo.AgtSpeciesExample;
import com.intellagric.service.agritech.SpeciesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: cwq
 * @Date: 2019/2/21 15:43
 * @Description:
 */
@Service
public class SpeciesServiceImpl implements SpeciesService {

    @Autowired
    private AgtSpeciesMapper agtSpeciesMapper;

    @Autowired
    private AgtSpeciesDao agtSpeciesDao;

    @Override
    public ResponseMessage addSpecies(AgtSpecies species) {
        species.setId(UUIDUtils.getID());
        species.setCreatedTime(new Date());
        return agtSpeciesMapper.insertSelective(species) == 1 ? ResponseMessage.success().add("add", species) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage deleteSpecies(String id) {
        return agtSpeciesMapper.deleteByPrimaryKey(id) == 1 ? ResponseMessage.success() : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editSpecies(AgtSpecies species) {
        return agtSpeciesMapper.updateByPrimaryKeySelective(species) == 1 ? ResponseMessage.success().add("update", species) : ResponseMessage.fail();
    }

    @Override
    public AgtSpecies getSpeciesById(String id) {
        return agtSpeciesMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getSpeciesList() {
        AgtSpeciesExample agtSpeciesExample = new AgtSpeciesExample();
        agtSpeciesExample.setOrderByClause("created_time ASC");
        agtSpeciesExample.createCriteria().andIdIsNotNull();
        List<AgtSpecies> agtSpeciesList = agtSpeciesMapper.selectByExample(agtSpeciesExample);
        return LayuiDataGridResult.success().add(agtSpeciesList, agtSpeciesList.size());
    }

    @Override
    public LayuiDataGridResult getSpeciesByPid(String pId) {
        AgtSpeciesExample agtSpeciesExample = new AgtSpeciesExample();
        agtSpeciesExample.setOrderByClause("created_time ASC");
        agtSpeciesExample.createCriteria().andPidEqualTo(pId);
        List<AgtSpecies> agtSpeciesList = agtSpeciesMapper.selectByExample(agtSpeciesExample);
        return LayuiDataGridResult.success().add(agtSpeciesList, agtSpeciesList.size());
    }

    @Override
    public LayuiDataGridResult getSpeciesTree() {
        //获取所有分类
        List<AgtSpecies> agtSpeciesList = this.getSpeciesList().getData();

        //获取顶级分类
        List<AgtSpecies> rootAgtSpeciesList = this.getSpeciesByPid("0").getData();

        for (AgtSpecies agtSpecies : rootAgtSpeciesList) {
            //每个分类项目都设置其子分类
            agtSpecies.setChildren(this.getChild(agtSpecies.getId(),agtSpeciesList));
        }

        return LayuiDataGridResult.success().add(rootAgtSpeciesList, rootAgtSpeciesList.size());
    }

    @Override
    public LayuiDataGridResult getSpeciesByPage(int page, int limit) {
        PageHelper.startPage(page, limit, "created_time ASC");
        AgtSpeciesExample speciesExample = new AgtSpeciesExample();
        speciesExample.createCriteria().andIdIsNotNull();
        List<AgtSpecies> speciesList = agtSpeciesMapper.selectByExample(speciesExample);
        return LayuiDataGridResult.success().add(speciesList, this.getSpeciesList().getCount());
    }

    @Override
    public LayuiDataGridResult getSpeciesListByVo(AgtSpeciesVo agtSpeciesVo, int page, int limit) {
        PageHelper.startPage(page, limit, "created_time DESC");
        List<AgtSpecies> speciesList = agtSpeciesDao.selectSpeciesListByVo(agtSpeciesVo);
        return LayuiDataGridResult.success().add(speciesList, this.getSpeciesList().getCount());
//        return LayuiDataGridResult.success().add(speciesList, speciesList.size());
    }


    @Override
    public List<AgtSpecies> getChild(String id, List<AgtSpecies> agtSpeciesList) {
        //返回的子菜单
        List<AgtSpecies> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (AgtSpecies agtSpecies : agtSpeciesList) {
            if (agtSpecies.getPid() != null) {
                //如果列表中的父id等于当前的分类的id，那就加入到该分类子列表中
                if (agtSpecies.getPid().equals(id)) {
                    childList.add(agtSpecies);
                }
            }
        }

        // 把子菜单的子菜单childList再循环一遍
        for (AgtSpecies agtSpecies : childList) {
            agtSpecies.setChildren(this.getChild(agtSpecies.getId(),agtSpeciesList));
        }

        //退出递归的条件
        if (childList.size() == 0) {
            return null;
        }

        return childList;
    }
}
